<?php
require_once 'Content.php';
require_once 'Articles.php';
class DB {
	private static $dbconnect;
	const FIRST_ROW = 0;
	const ERROR_MSG = "Error : ";
	const ENCODING = "utf8";
	
	
	function __construct() {
		if (!self::$dbconnect){
			self::$dbconnect=  	mysql_connect("localhost","root","")
				or die("Could not connect: ".mysql_error());
		
			mysql_select_db("db_mk", self::$dbconnect)
				or die("Could not find database: ".mysql_errno());
		
			mb_internal_ENCODING("UTF-8");
			mysql_query("SET 	character_set_results = '".self::ENCODING."',
							 	character_set_client = '".self::ENCODING."', 
								character_set_connection = '".self::ENCODING."', 
								character_set_database = '".self::ENCODING."', 
								character_set_server = '".self::ENCODING."';", self::$dbconnect);
		}
		
	}
	
	function addImage( IProject $image) {
		
		mysql_query("insert into 	gallery	(gal_order, gal_file_name, gal_description, gal_header,gal_date)
									values		(".$image->getOrder()." ,'".$image->getName()."','".$image->getText()."',
												".$image->getImgToHeader().",'".$image->getDate()."')",self::$dbconnect)
										or die (self::ERROR_MSG.mysql_error());
	}
	function addArticle ( IProject $article) {
		mysql_query("insert into 	articles	(articles_name,		articles_text)
									values		('".$article->getName()." ',	'".$article->getText()."'	)",self::$dbconnect)
				or die (self::ERROR_MSG.mysql_error());	
	}
	
	function addNews ( IProject $news) {
		mysql_query("	insert into 	news  (n_id,	news_name,		news_order,		news_text,		news_vis,	news_image_name		)
										values(".$news->getId().",	'".$news->getName()."',	".$news->getOrder().",	'".$news->getText()."',	".$news->getVisible().",	'".$news->getImage()."'	)",self::$dbconnect)
				or die (self::ERROR_MSG.mysql_error());
	}
	
	function allHeaderNews(){
		$quary = mysql_query("	select 	news_name,
										news_text,
										news_vis,		
										news_order,			
										n_id ,	
										news_image_name
								from 	news
								where 	news_vis = 1
								order by news_order",self::$dbconnect);
		$arr= array();
		while ($row = mysql_fetch_object($quary)){
			$news = new News($row->news_name, $row->news_text, $row->news_image_name,$row->news_order,$row->news_vis);
			$news->setId($row->n_id);
			array_push($arr,$news);
		}
		return $arr;
		
	}
	
	function allHeaderImages(){
		$quary = mysql_query("	select 	gal_id,
										gal_file_name,
										gal_description, 
										gal_date,
										gal_header,
										gal_order
								from 	gallery
								where 	gal_header = 1",self::$dbconnect);
		$arr= array();
	while ($row = mysql_fetch_object($quary)){
			$img = new Image($row->gal_file_name, $row->gal_description, $row->gal_date,$row->gal_header,$row->gal_order);
			$img ->setId($row->gal_id);
			array_push($arr,$img);
		}
		return $arr;
		
	}
	
	function findName($table){
		$quary = mysql_query("	select ".$table."_name
								from $table
								order by ".$table."_name",self::$dbconnect);
		$i=0;
		$arr=Array();
		while ($arrdb = mysql_fetch_row($quary)){
			$arr[$i]=$arrdb[0];
			$i++;
		}
		return $arr;
	}
	
	function takeArticleName($numb){
		$quary = mysql_query("	select  articles_name
								from articles
								where a_id = $numb	",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
		$text = mysql_result($quary, self::FIRST_ROW, "articles_name");
		return $text;
	
	}
	
	
	

	
	function takeArticleId($name){
		$quary = mysql_query("	select a_id
								from articles
								where articles_name = '$name'",self::$dbconnect)
				or die (self::ERROR_MSG.mysql_error());
		return mysql_result($quary, self::FIRST_ROW , "a_id");
	}
	
	function extractArticle($name){
		require_once ('Articles.php');
		$article = new Article($name);
		$quary = mysql_query("	select 	a_id,
										articles_name,
										articles_text
								from 	articles
								where 	articles_name = '$name'",self::$dbconnect)
									or die (self::ERROR_MSG.mysql_error());
		
		$article->setId(mysql_result($quary, self::FIRST_ROW , "a_id"));
		$article->setText(mysql_result($quary, self::FIRST_ROW , "articles_text"));
		
		return $article;
	}
	
	function extractNews($name){
		require_once ('News.php');
		
		$quary = mysql_query("	select 	news_text,
										news_vis,		
										news_order,			
										n_id ,	
										news_image_name
								from 	news
								where 	news_name = '$name'",self::$dbconnect)
										or die (self::ERROR_MSG.mysql_error());
	
		$news = new News(	$name,
				 			mysql_result($quary, self::FIRST_ROW , "news_text"),
							mysql_result($quary, self::FIRST_ROW , "news_image_name"),
						 	mysql_result($quary, self::FIRST_ROW , "news_order"),
							mysql_result($quary, self::FIRST_ROW , "news_vis"));
		$news->setId(mysql_result($quary, self::FIRST_ROW , "n_id"));
	
		return $news;
	}

	function extractImage($name){
		require_once ('Image.php');
		
		$quary = mysql_query("	select 	gal_description, 
										gal_date,
										gal_header,
										gal_order,
										gal_id
								from 	gallery
								where 	gal_file_name = '$name'",self::$dbconnect)
									or die (self::ERROR_MSG.mysql_error());
								
			$img = new Image(	$name,
								mysql_result($quary, self::FIRST_ROW ,"gal_description"),
								mysql_result($quary, self::FIRST_ROW , "gal_date"),
								mysql_result($quary, self::FIRST_ROW , "gal_header"),
								mysql_result($quary, self::FIRST_ROW , "gal_order"));
			$img->setId(mysql_result($quary, self::FIRST_ROW , "gal_id"));
					
		return $img;
	}
	

	
	function updateArticles( IProject $article){
		
		$quary = mysql_query("	update articles 
								set articles_text = '".$article->getText()."',
									articles_name = '".$article->getName()."'
								where a_id = ".$article->getId()."  ",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
	}
	
	function updateNews( IProject $news) {
		$quary=mysql_query("SET foreign_key_checks = 0",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
		$quary = mysql_query("	update news
								set news_text = '".$news->getText()."',
									news_name = '".$news->getName()."',		
									news_order = ".$news->getOrder().",			
									news_vis = ".$news->getVisible().",	
									news_image_name = '".$news->getImage()."'	
									where n_id = ".$news->getId()."  ",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
		$quary=mysql_query("SET foreign_key_checks = 1",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
	}
	function updateImage( IProject $image) {
		$quary = mysql_query("	update gallery
								set gal_description ='".$image->getText()."',
									gal_date='".$image->getDate()."',
									gal_header=".$image->getImgToHeader().",
									gal_order=".$image->getOrder()."
								where gal_id = ".$image->getId()."  ",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
	}

	public function deleteRecordInArticles( IProject $n){
		$quary = mysql_query("delete from articles where a_id = ".$n->getId(),self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
	}
	
	public function deleteRecordInNews( IProject $n){
		$quary = mysql_query("delete from news where n_id = ".$n->getId(),self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
	}
	public function deleteRecordInGallery( IProject $n){
		$quary=mysql_query("SET foreign_key_checks = 0",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
		$quary = mysql_query("delete from gallery where gal_id = ".$n->getId(),self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
		$quary = mysql_query("UPDATE news SET news_image_name = 'no_photo.jpg' WHERE news_image_name = '".$n->getName()."'",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
		$quary=mysql_query("SET foreign_key_checks = 1",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
	}
	public function takeGallery(){
		require_once 'Image.php';
		$quary = mysql_query(" select 	gal_file_name,
										gal_description, 
										gal_date,
										gal_header,
										gal_order
								from 	gallery
								order by gal_order",self::$dbconnect) or die (self::ERROR_MSG.mysql_error());
		$arr= array();
		while ($row = mysql_fetch_object($quary)){
			$img = new Image($row->gal_file_name, $row->gal_description, $row->gal_date,$row->gal_header,$row->gal_order);
			array_push($arr,$img);
		}
		
		return $arr;				
	}
	public function printImgAllInfo($arr){
		echo"<table border='1'>";
		$n=count($arr);
		echo "<tr>";
		for ($i=0;$i<$n;$i++){			
			echo "\n <td><a href='?page=edit_image&image=".$arr[$i]->getName()."'><img src='images/gallery/".$arr[$i]->getName()."' width='80' height='80'></a><br/>".$arr[$i]->getDate()."</td>";
			if (($i+1) % 10 == 0 && ($i+1)!=$n ) echo "\n</tr>\n<tr>\n";
		}
		echo "</tr>";
		echo "</table>";
	}
	
	public function lastNewsId(){
		$quary = mysql_query("	select 	news_order
								from 	news
								order by n_id DESC
								limit 1",self::$dbconnect)
										or die (self::ERROR_MSG.mysql_error());
		return mysql_result($quary, self::FIRST_ROW , "news_order");
	}
	public function reOrderObjArray($objArray,$idx,$newOrder){
		
		$oldOrder = intval($objArray[$idx]->getOrder());
		$n=count($objArray);
		$newOrder = intval($newOrder);
		if ($newOrder>$n){
			$newOrder=$n;
		}
		if ($newOrder<1){
			$newOrder=1;
		}
		
 		if ($n >=$newOrder){
			if ($oldOrder<$newOrder){				
				
				for ($i=0;$i<$n;$i++){
					if (intval($objArray[$i]->getOrder()) > $oldOrder && intval($objArray[$i]->getOrder()) <= $newOrder ) {
						$objArray[$i]->setOrder(intval($objArray[$i]->getOrder())-1);
					}
				}
				$objArray[$idx]->setOrder($newOrder);


			} else {
				
				for ($i=0;$i<$n;$i++){
					if (intval($objArray[$i]->getOrder()) < $oldOrder && intval($objArray[$i]->getOrder()) >= $newOrder ) {
						$objArray[$i]->setOrder(intval($objArray[$i]->getOrder())+1);
					}
				}
				$objArray[$idx]->setOrder($newOrder);			
			}
			
		}  
		return $objArray;
	}
	
	
	
}
?>